//
//  HMDropDownView.m
//  HMMeituan19HD
//
//  Created by 邢广 on 16/9/16.
//  Copyright © 2016年 xg. All rights reserved.
//

#import "HMDropDownView.h"
#import "HMcetegoryModel.h"
#import "HMcetegoryModel.h"
#import "HMDropDownLiftCell.h"
#import "HMDropDownRightCell.h"

@interface HMDropDownView() <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *lefttabView;
@property (weak, nonatomic) IBOutlet UITableView *righttabView;

@property (nonatomic, strong) HMcetegoryModel *selectCategoryLeftModel;

@end

@implementation HMDropDownView

+ (instancetype)dropDownView {
    
    return [[NSBundle mainBundle]loadNibNamed:@"HMDropDownView" owner:nil options:nil].firstObject;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    self.autoresizingMask = UIAccessibilityTraitNone;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.lefttabView) {
        return self.cetegoryArray.count;
    } else {
        return self.selectCategoryLeftModel.subcategories.count;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.lefttabView) {
        
        //左边表格
        HMDropDownLiftCell *cell = [HMDropDownLiftCell dropDownLiftCellWithTabView:tableView];
        
        //1. 显示Label数据
        HMcetegoryModel *categoryModel = self.cetegoryArray[indexPath.row];
        cell.textLabel.text = categoryModel.name;
        
        //2. 显示Image数据
        cell.imageView.image = [UIImage imageNamed:categoryModel.icon];
        
        //3. 设置cell的附属视图
        if (categoryModel.subcategories.count == 0) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        } else {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        
        
        return cell;
        
    } else {
        
        //右边表格
        HMDropDownRightCell *cell = [HMDropDownRightCell dropDownRightCellWithTabView:tableView];
        
        // 为了显示右边的数据  --> 需要知道左边选中了谁 --> tableView的选中方法中记录
        NSArray *categoryArray = self.selectCategoryLeftModel.subcategories;
        
        cell.textLabel.text = categoryArray[indexPath.row];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.lefttabView) {
        
        //1. 记录左边选中的模型 
        self.selectCategoryLeftModel = self.cetegoryArray[indexPath.row];
        
        //2. 刷新右边数据
        [self.righttabView reloadData];
    }
    
}


@end
